package com.sinosoft.svc.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;

import smsmessage.sms.SMSclientSubmit;

import com.sinosoft.svc.entity.SMSSendEntity;


public final class SMSUtils {

	private static Logger logger = Logger.getLogger(SMSUtils.class);


	public static String sendMessage(String phone,String message){
		synchronized (RCUtils.class) {
		SMSclientSubmit mt = new SMSclientSubmit();///实例化用于下发短信的对象
		
		String massTel = phone;//群发以逗号分割，最多100个
		mt.setDestID(massTel);//接收号码
		mt.setPriority(1);// 设置优先级，默认为1
		mt.setMsgContent(message);// 短信内容
		String errorMessage = "true";
		
		List<SMSSendEntity> smsSendEntities = new ArrayList<SMSSendEntity>();
		List<String> sendIDList = new ArrayList<String>();
		sendIDList.add("1");
		try {
			while ("1".equals(sendIDList.get(0))
					|| "2".equals(sendIDList.get(0))
					|| "3".equals(sendIDList.get(0))
					|| "4".equals(sendIDList.get(0))
					|| "5".equals(sendIDList.get(0))
					|| "6".equals(sendIDList.get(0))) {
				sendIDList = mt.sendSMSList();
				if ("1".equals(sendIDList.get(0))) {
					logger.error("信息下发缓冲队列已满");
					errorMessage = "报错,信息下发缓冲队列已满";
					break;
				} else if ("2".equals(sendIDList.get(0))) {
					logger.error("没有建立连接");
					errorMessage = "报错,没有建立连接";
					break;
				} else if ("3".equals(sendIDList.get(0))) {
					logger.error("流量超出设定值");
					errorMessage = "报错,流量超出设定值";
					break;
				} else if ("4".equals(sendIDList.get(0))) {
					logger.error("短信长度超出限制");
					errorMessage = "报错,短信长度超出限制";
					break;
				} else if ("5".equals(sendIDList.get(0))) {
					logger.error("群发数量超出");
					errorMessage = "报错,群发数量超出";
					break;
				} else if ("6".equals(sendIDList.get(0))) {
					logger.error("分隔符不正确");
					errorMessage = "报错,分隔符不正确";
					break;
				} else {
					String[] arg = massTel.split(",");
					Date date = new Date();
					for (int i = 1; i < sendIDList.size(); i++) {
						logger.info("企业下行消息标识 [" + sendIDList.get(i) + "],"
								+ "目的号码 ["
								+ arg[i - 1] + "]," + "短信内容 ["
								+ mt.getMsgContent() + "]");
						
						SMSSendEntity smsSendEntity = new SMSSendEntity();
						String id = null;
						id = UUID.randomUUID().toString();
						smsSendEntity.setId(id);
						smsSendEntity.setRetVal(sendIDList.get(i));
						smsSendEntity.seteId(arg[i - 1]);
						smsSendEntity.setMsg(mt.getMsgContent());
						smsSendEntity.setSendTime(date);
						smsSendEntities.add(smsSendEntity);
					}
				}
			}
			if(smsSendEntities.size()>0){
				JSONArray jsonArray = JSONArray.fromObject(smsSendEntities);
				RCUtils.class.notify();
				return errorMessage+","+jsonArray.toString();
			}else{
				RCUtils.class.notify();
				return errorMessage;
			}
		} catch (Exception e) {
			RCUtils.class.notify();
			return errorMessage;
		}
		
	}
	} /*发送end*/
	
	
}
